﻿using QFramework;
using System.Net;

namespace HKTools
{
    public class CMD_UDP_UnicastMsg : AbstractCommand
    {
        string msg;
        string address;
        int port;

        public CMD_UDP_UnicastMsg(string msg, string address, int port)
        {
            this.msg = msg;
            this.address = address;
            this.port = port;
        }

        protected override void OnExecute()
        {
            if (string.IsNullOrEmpty(msg))
            {
                HKDebug.LogError("Msg 不能为空");
                return;
            }

            // 校验端口号是否合法
            if (port < 0 || port > 65535)
            {
                HKDebug.LogError("端口号必须在 0 ~ 65535 之间！");
                return;
            }

            // 校验IP地址是否合法
            if (!IPAddress.TryParse(address, out IPAddress ipAddress))
            {
                HKDebug.LogError("IP 地址格式错误！");
                return;
            }

            IPEndPoint remotePoint = new IPEndPoint(ipAddress, port);

            this.GetSystem<IHK_UdpSystem>().UnicastMsg(msg, remotePoint);
        }
    }
}
